﻿using MediatR;
using Microsoft.Data.SqlClient;
using S.Authorization.API.Application.Command.UserCommand;
using S.Authorization.Common.Utilities;
using S.Authorization.Infrastructure;
using System.Security.Policy;

namespace S.Authorization.API.Application.CommandHandlers.UserCommandHandler
{
    public class UserUpdateCommandHandler : IRequestHandler<UserUpdateCommand, bool>,IRequestHandler<UserUpdatePassWordCommand, bool>,IRequestHandler<UserUpdatePhoneCommand,bool>
    {
        private readonly DbHelper db;
        public UserUpdateCommandHandler(DbHelper _db)
        {
            this.db = _db;
        }
        public async Task<bool> Handle(UserUpdateCommand request, CancellationToken cancellationToken)
        {
            string sql = @"UPDATE UsersModel SET UserName=@UserName,UpdateBy=@UpdateBy,UpdateDate=@UpdateDate WHERE UserId=@UserId";
            var parameters = new List<SqlParameter>
                {
                     new SqlParameter("@UserName", request.Name),
                     new SqlParameter("@UserId", request.ID),
                     new SqlParameter("@UpdateBy", request.UpdateBy),
                     new SqlParameter("@UpdateDate", DateTime.Now)
                };
            int result = await db.ExecuteNonQuery(sql, parameters.ToArray());
            return result > 0;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<bool> Handle(UserUpdatePassWordCommand request, CancellationToken cancellationToken)
        {
            #region MD5加密
            string hash = Md5Helper.CalculateMd5Hash(request.PassWord);
            #endregion
            string sql = @"UPDATE UsersModel SET UserPassword=@UserPassword,UpdateBy=@UpdateBy,UpdateDate=@UpdateDate WHERE UserId=@UserId";
            var parameters = new List<SqlParameter>
                {
                     new SqlParameter("@UserPassword",hash),
                     new SqlParameter("@UserId", request.ID),
                     new SqlParameter("@UpdateBy", request.UpdateBy),
                     new SqlParameter("@UpdateDate", DateTime.Now)
                };
            int result = await db.ExecuteNonQuery(sql, parameters.ToArray());
            return result > 0;
        }
        /// <summary>
        /// 修改手机号
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> Handle(UserUpdatePhoneCommand request, CancellationToken cancellationToken)
        {
            string sql = @"UPDATE UsersModel SET UserPhone=@UserPhone,UpdateBy=@UpdateBy,UpdateDate=@UpdateDate WHERE UserId=@UserId";
            var parameters = new List<SqlParameter>
            {
                     new SqlParameter("@UserPassword", request.Phone),
                     new SqlParameter("@UserId", request.ID),
                     new SqlParameter("@UpdateBy", request.UpdateBy),
                     new SqlParameter("@UpdateDate", DateTime.Now)
                };
            int result = await db.ExecuteNonQuery(sql, parameters.ToArray());
            return result > 0;
        }
    }
}
